/**
 * Created by liuqingxia on 2016/2/25.
 */

var url = getRoot() + "/admin/user/find";
var id;
var userSqlTable;
var userSqlTableUrl=getRoot() + "/admin/getUserSql";
$(document).ready(function () {
	//ajax请求角色列表
	//ajaxRequestRoleList();
	
    LTS.colFormatter.optFormat = function (v,row) {
        return '<a href="javascript:;" class="update-btn"><span class="label label-success" style="font-size: 14px"><i class="fa fa-edit"></i>编辑</span><span class="hidden lts-data">' + JSON.stringify(row) + '</span></a>&nbsp;' +
            '<a href="javascript:;" class="remove-btn"><span class="label label-primary" style="background-color: #DD6B55;font-size: 14px"><i class="fa fa-trash-o"></i>删除</span></a>&nbsp'+
        '<a href="javascript:;" class="show-user-sql-btn"><span class="label label-primary" style="font-size: 14px"><i class="fa fa-search"></i>查看Ta的SQL</span></a>&nbsp;';
    }

    var ltsTable = $("#user-table-container").ltsTable({
        url: url,
        templateId: 'user-table'
    });
    userSqlTable=$("#user-sql-table-container").ltsTable({
    	url: userSqlTableUrl,
        templateId: 'user-sql-table'
    })
    $(document).on("click", "#searchBtn", function () {
        var params = {};
        $.each($('#user-query-form').find(".form-control"), function () {
            var name = $(this).attr("name");
            if(isNull(name)){
                return true;
            }
            var value = $(this).val();
            if(!isNull(value)){
                value = $.trim(value);
                params[name] = value;
            }
        });
        ltsTable.post(params, 1);
    });

    $(document).on("click",".remove-btn",function(){
        id = $(this).parent().parent().find(".id-tr").find(".id-span").html();
        $("#remove-tip-modal").modal("show");
    });

    $(document).on("click","#confirm-remove-btn",function(){
        var params = {};
        params['id'] = $.trim(id);
        var url = getRoot() + '/admin/user/remove';
        $.postHttp(params,url,function(json){
            if (json && json.success) {
                $("#cancel-remove-btn").trigger("click");
                swal("成功",json['message'],"success");
                ltsTable.post({}, pageNum);
                //$("#searchBtn").trigger("click");
            } else {
                if (json) {
                    $("#remove-tip-modal").hide();
                    swal("失败",json['message'], 'error');
                }
            }
        });
        id = "";
    });


    $(document).on("click",".update-btn",function(){
        var userJson = $(this).find("span.lts-data").html();
        userJson = $.trim(userJson);
        var user = JSON.parse(userJson);
        $.each($('#editForm').parent().find(".form-control"), function () {
            var name = $(this).attr("name");
            if(isNull(name)){
                return true;
            }
            name = $.trim(name);
            var value = user[name];
            $(this).val(value);
        });
        $("#user-edit-modal").modal("show");
    });

    $(document).on("click", "#edit-btn", function () {
        var params = {};
        $.each($('#editForm').parent().find(".form-control"), function () {
            var name = $(this).attr("name");
            if(isNull(name)){
                return true;
            }
            var value = $(this).val();
            if(!isNull(value)){
                value = $.trim(value);
                params[name] = value;
            }
        });
        params['roleId'] = $("#userOptId option:selected").attr("id");
        var url = getRoot() + "/admin/user/update";
        $.postHttp(params,url,function(json){
            if (json && json.success) {
                swal("成功",json['message'],"success");
                $("#user-edit-modal").modal("hide");
                ltsTable.post({}, pageNum);
                //$("#searchBtn").trigger("click");
            } else {
                if (json) {
                    swal('失败',json['message'], 'error');
                }
            }
        });
    });
    $("#searchBtn").trigger("click");
    showUserSqlBtn();
});
function showUserSqlBtn(){
	$(document).on("click",".show-user-sql-btn",function(){
		var currentId = getCurrentId($(this));
		var param={
				id:Number(currentId)
		}
		userSqlTable.post(param,1);
		$("#user-sql-modal").modal("show");
	})
}
function getCurrentId(ele) {
    var id = ele.parent().parent().find(".id-tr").find(".id-span").html();
    return id;
}